#! /usr/bin
#-*- coding:utf-8 -*-

__author__ = 'Michael'


import os, sys
import logging
import logging.handlers

#config
max_bytes = 1024*1024
b_count = 5

LEVELS = {'debug': logging.DEBUG,
          'info': logging.INFO,
          'warning': logging.WARNING,
          'error': logging.ERROR,
          'critical': logging.CRITICAL}

class log:
	# level 可以输出的最低级别
	# type	0 输出到前台和文件 1 输出到文件
	def __init__(self, name, level = 'debug', type = 0):
		debug_file = name+"-debug.log"
		error_file = name+"-error.log"

		self.debug_logger = logging.getLogger(debug_file)
		self.error_logger = logging.getLogger(error_file)

		level = LEVELS.get(level, logging.NOTSET)
		self.debug_logger.setLevel(level)

		dfh = logging.handlers.RotatingFileHandler(debug_file, maxBytes=max_bytes, backupCount = b_count)
		efh = logging.handlers.RotatingFileHandler(error_file, maxBytes=max_bytes, backupCount = b_count)

		formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")

		dfh.setFormatter(formatter)
		efh.setFormatter(formatter)

		self.debug_logger.addHandler(dfh)
		self.error_logger.addHandler(efh)

		if type == 0:
			ch = logging.StreamHandler()
			ch.setFormatter(formatter)
			self.debug_logger.addHandler(ch)
			self.error_logger.addHandler(ch)

	def debug(self, message):
		self.debug_logger.debug(message)

	def info(self, message):
		self.debug_logger.info(message)

	def error(self, message):
		self.error_logger.error(message)


def test():
	argn = len(sys.argv)
	if argn > 2:
		mylog = log("test", sys.argv[1], sys.argv[2])
	elif argn > 1:
		mylog = log("test", sys.argv[1])
	else:
		mylog = log("test")

	print "Log testing ..."
	mylog.debug('This is a debug message')
	mylog.info('This is an info message')
	mylog.error('This is an error message')

